<template>
  <el-form :model="formData" size="small" :rules="rules" ref="formRef" label-width="84px">
    <el-form-item label="名称" prop="name">
      <el-input v-model="formData.name" :maxlength="32"></el-input>
    </el-form-item>
    <el-form-item label="描述" prop="remark">
      <el-input type="textarea" v-model="formData.remark"></el-input>
    </el-form-item>
    <el-form-item>
      <el-button type="primary" @click="handleConfirm">保存</el-button>
      <el-button @click="handleReset">重置</el-button>
      <el-button @click="handleClose">关闭</el-button>
    </el-form-item>
  </el-form>
</template>

<script>
import { stateOptions } from "@/constant/defaultOptions.js";
import { createLoading, createMessage } from "@/utils/index.js";
import { goBack } from "@/utils/browser.js";
export default {
  name: "notice_add",
  data() {
    return {
      stateOptions,
      formData: {
        name: "",
        remark: "",
      },
      rules: {
        name: [{ required: true, message: "必填", trigger: "blur" }],
      },
    };
  },
  methods: {
    handleConfirm() {
      this.$refs.formRef.validate(async (isOk) => {
        if (!isOk) {
          return createMessage("请完成必填项的填写与选择");
        }
        const params = {
          ...this.formData,
        };
        const loading = createLoading("保存中，请稍等");
        try {
          await this.$globalApi.notice.sysNoticeInsertApi(params);
          createMessage("保存成功", { type: "success" });
          this.$emit("refresh");
          goBack("/notice");
        } catch (err) {
          console.log("新增报错了", err);
        } finally {
          loading.close();
        }
      });
    },
    handleReset() {
      this.$refs.formRef.resetFields();
    },
    handleClose() {
      goBack("/notice");
    },
  },
};
</script>

<style></style>